package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzService clazzService;

    @Override
    public PageResult<Student> listStudent(StudentQueryParam student) {
        Page<Student> studentPage = Page.of(student.getPage(), student.getPageSize());
        lambdaQuery()
            .like(student.getName() != null && !student.getName().isEmpty(), Student::getName, student.getName())
            .eq(student.getDegree() != null, Student::getDegree, student.getDegree())
            .eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId())
            .page(studentPage);
        List<Student> studentList = studentPage.getRecords();
        List<Integer> clazzIdList = studentList.stream().map(student1 -> student1.getClazzId()).collect(Collectors.toList());
        log.info("===================" + clazzIdList);
        if(!clazzIdList.isEmpty()){
            Map<Integer, String> clazzIdNameMap = clazzService.listByIds(clazzIdList).stream().collect(Collectors.toMap(Clazz::getId, Clazz::getName));
            studentPage.getRecords().stream().forEach(student1 -> student1.setClazzName(clazzIdNameMap.getOrDefault(student1.getClazzId(),"null")));
            log.info("===================" + studentPage.getRecords());
        }
        return new PageResult<>(studentPage.getTotal(), studentList);
    }

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        save(student);
    }

    @Override
    public Student getByIdStudent(Integer id) {
        Student student = getById(id);
        return student;
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        lambdaUpdate()
            .eq(student.getId() != null, Student::getId, student.getId())
            .update(student);
    }

    @Override
    public void deleteStudent(List<Integer> ids) {
        removeByIds(ids);
    }

    @Override
    public void violation(Integer id, Integer score) {
        lambdaUpdate()
            .eq(Student::getId, id)
            .setSql("violation_count = violation_count + " + 1)
            .setSql("violation_score = violation_score + " + score)
            .update();
    }

}
